//
//  EFShoppingCartCell.m
//  EnjogFood
//
//  Created by 张文瀚 on 2017/5/1.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFShoppingCartCell.h"
#import "EFShoppingCartItem.h"
#import "EFSepecialItem.h"
#import "iToast.h"

@interface EFShoppingCartCell ()
@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIButton *selectButton;
@property(nonatomic, strong)UIImageView *iconImgView;
@property(nonatomic, strong)UILabel *productNameLabel;
@property(nonatomic, strong)UILabel *priceLabel;
@property(nonatomic, strong)UILabel *discountPriceLabel;
@property(nonatomic, strong)UIButton *addButton;
@property(nonatomic, strong)UIButton *reduceButton;
@property(nonatomic, strong)UILabel *countLabel;
@property(nonatomic, strong)UIView *bottomLine;
//invalid
@property(nonatomic, strong)UIImageView *invalidTitleImgView;
@property(nonatomic, strong)UILabel *invalidTitleLabel;
//@property(nonatomic, strong)UILabel *invalidTipLabel;
@property(nonatomic, strong)UILabel *invalidReasonLabel;

@property (strong, nonatomic) EFShoppingCartItem *shopItem;

@end

@implementation EFShoppingCartCell


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor =  [UIColor clearColor];
        [self initView];
    }
    return self;
}

- (void)initView{
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.selectButton];
    [self.bgView addSubview:self.iconImgView];
    [self.bgView addSubview:self.productNameLabel];
    [self.bgView addSubview:self.priceLabel];
    [self.bgView addSubview:self.discountPriceLabel];
    [self.bgView addSubview:self.addButton];
    [self.bgView addSubview:self.countLabel];
    [self.bgView addSubview:self.reduceButton];
    [self.bgView addSubview:self.bottomLine];
    
    [self.contentView addSubview:self.invalidTitleImgView];
    [self.contentView addSubview:self.invalidTitleLabel];
//    [self.bgView addSubview:self.invalidTipLabel];
    [self.bgView addSubview:self.invalidReasonLabel];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.selectButton.centerY = self.bgView.height/2.f;
    self.selectButton.left = scale(18.f);
    self.iconImgView.left = self.selectButton.right + scale(10.f);
    self.iconImgView.centerY = self.bgView.height/2.f;
    
    [self refreshCountUI];
    
    self.productNameLabel.top = self.iconImgView.top;
    self.productNameLabel.left = self.iconImgView.right + scale(10.f);
    
    self.priceLabel.left = self.productNameLabel.left;
    self.priceLabel.bottom = self.iconImgView.bottom;
    
    self.discountPriceLabel.left = self.priceLabel.right + scale(10.f);
    self.discountPriceLabel.bottom = self.iconImgView.bottom;
    
    self.bottomLine.bottom = self.bgView.height;
    
    //invalid
    
    self.invalidTitleLabel.centerY = 15.f;
    self.invalidTitleLabel.centerX = kScreenWidth/2.f + 14.f;

    self.invalidTitleImgView.centerY = 15.f;
    self.invalidTitleImgView.right = self.invalidTitleLabel.left - 5.f;
    
    self.invalidReasonLabel.left = self.productNameLabel.left;
    self.invalidReasonLabel.bottom = self.iconImgView.bottom;
    
    
}

- (void)setObject:(EFShoppingCartItem *)item{
    [super setObject:item];
    self.shopItem = item;
    self.invalidTitleLabel.hidden = item.invalid && item.invalidFirst ? NO : YES;
    self.invalidTitleImgView.hidden = self.invalidTitleLabel.hidden;
    self.bgView.top = item.invalid && item.invalidFirst ? 15.f : 0.f;
    if (item.invalid) {
        
        
        self.bgView.backgroundColor = RGBCOLOR(0xF5F9F9);
        self.selectButton.enabled = NO;
        
        self.priceLabel.hidden = YES;
        self.discountPriceLabel.hidden = YES;
        self.addButton.hidden = YES;
        self.countLabel.hidden = YES;
        self.reduceButton.hidden = YES;
        
        self.invalidReasonLabel.hidden = NO;
        
        
    }else{
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.selectButton.enabled = YES;
        self.selectButton.selected = item.selected;
        
        self.priceLabel.hidden = NO;
        self.discountPriceLabel.hidden = NO;
        self.addButton.hidden = NO;
        self.countLabel.hidden = NO;
        self.reduceButton.hidden = NO;
        
        self.invalidReasonLabel.hidden = YES;
    }
    
    
    self.iconImgView.backgroundColor = [UIColor clearColor];
    [self.iconImgView setImageWithURL:[NSURL URLWithString:item.img] placeholderImage:nil];
    self.productNameLabel.text = item.productName;
    self.productNameLabel.width = kScreenWidth - self.iconImgView.width - self.selectButton.width - scale(58.f);
    [self.productNameLabel sizeToFit];
    if (self.productNameLabel.height > self.iconImgView.height - self.addButton.height - 5.f) {
        self.productNameLabel.height = self.iconImgView.height - self.addButton.height - 5.f;
    }
    
    self.priceLabel.text = item.salePrice;
    [self.priceLabel sizeToFit];
    
    self.discountPriceLabel.text = item.price;
    [self.discountPriceLabel sizeToFit];
    
    self.countLabel.text =  [NSString stringWithFormat:@"%@",item.qty];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self.countLabel sizeToFit];
    
    self.invalidReasonLabel.text = item.reasonMessage;
    [self.invalidReasonLabel sizeToFit];
    if (self.invalidReasonLabel.width > kScreenWidth - self.iconImgView.width - self.selectButton.width - scale(58.f)) {
        self.invalidReasonLabel.width = kScreenWidth - self.iconImgView.width - self.selectButton.width - scale(58.f);
    }
    
}


- (void)onClickSelectBtn{
    self.selectButton.selected = !self.selectButton.selected;
    if(self.selectBlock){
        self.selectBlock(self.selectButton.selected);
    }
}

- (void)onClickReduceBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count --;
    if (count <= 0) {
        count = 0;
    }
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    
    if(self.changeBlock){
        self.changeBlock(count);
    }
    [self refreshCountUI];
    //进行服务器请求
    EFSepecialItem *sItem = [self.shopItem castToSpecialItem];
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [APP_CONTEXT.net subCart:sItem.id qty:[NSNumber numberWithInt:1] type:sItem.type barcode:sItem.barcode OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            if (code != 0) {
                [[iToast makeText:message] show];
            }else{
                //本地减少
                if (sItem.type.integerValue == 0) {
                    [APP_CONTEXT.shoppingCartManager subStorProduct:sItem];
                }else{
                    [APP_CONTEXT.shoppingCartManager subScanProduct:sItem];
                }
            }
        }];
    }else{
        //本地减少
        if (sItem.type.integerValue == 0) {
            [APP_CONTEXT.shoppingCartManager subStorProduct:sItem];
        }else{
            [APP_CONTEXT.shoppingCartManager subScanProduct:sItem];
        }
        
    }
}

- (void)onClickAddBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count ++;
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    
    if(self.changeBlock){
        self.changeBlock(count);
    }
    [self refreshCountUI];
     EFSepecialItem *sItem = [self.shopItem castToSpecialItem];
    //进行服务器请求
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [APP_CONTEXT.net AddCartId:sItem.id qty:[NSNumber numberWithInt:1] type:sItem.type OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
            if (code != 0) {
                [[iToast makeText:message] show];
            }else{
                 [[iToast makeText:@"成功加入购物车"] show];
                //本地增加
                if (sItem.type.integerValue == 0) {
                    [APP_CONTEXT.shoppingCartManager addStorProduct:sItem];
                }else{
                    [APP_CONTEXT.shoppingCartManager addScanProduct:sItem];
                }
                
            }
        }];
    }else{
        //存在本地 当用户点击购物车时 sync一下
         [[iToast makeText:@"成功加入购物车"] show];
        
        
        //本地增加
        if (sItem.type.integerValue == 0) {
            [APP_CONTEXT.shoppingCartManager addStorProduct:sItem];
        }else{
            [APP_CONTEXT.shoppingCartManager addScanProduct:sItem];
        }
        
    }

}

- (void)refreshCountUI{
    self.addButton.right = kScreenWidth - scale(20.f);
    self.addButton.bottom = self.iconImgView.bottom;
    self.countLabel.right = self.addButton.left - scale(10.f);
    self.countLabel.centerY = self.addButton.centerY;
    self.reduceButton.right = self.countLabel.left - scale(10.f);
    self.reduceButton.bottom = self.addButton.bottom;
}


- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 120.f)];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIButton *)selectButton{
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22.f, 22.f)];
        [_selectButton setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_selectButton setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_selectButton setImage:IMG(@"cartlist_failure_icon") forState:UIControlStateDisabled];
        [_selectButton addTarget:self action:@selector(onClickSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

- (UIImageView *)iconImgView{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80.f, 80.f)];
    }
    return _iconImgView;
}

- (UILabel *)productNameLabel{
    if (!_productNameLabel) {
        _productNameLabel = [[UILabel alloc] init];
        _productNameLabel.backgroundColor = [UIColor clearColor];
        _productNameLabel.font = [UIFont systemFontOfSize:15.f];
        _productNameLabel.textColor = kBlack333;
        _productNameLabel.numberOfLines = 0;
    }
    return _productNameLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.font = [UIFont systemFontOfSize:13.f];
        _priceLabel.textColor = RGBCOLOR(0x69BA45);
    }
    return _priceLabel;
}


- (UILabel *)discountPriceLabel{
    if (!_discountPriceLabel) {
        _discountPriceLabel = [[UILabel alloc] init];
        _discountPriceLabel.backgroundColor = [UIColor clearColor];
        _discountPriceLabel.font = [UIFont systemFontOfSize:10.f];
        _discountPriceLabel.textColor = RGBCOLOR(0xCCCCCC);
    }
    return _discountPriceLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.backgroundColor = [UIColor clearColor];
        _countLabel.font = [UIFont systemFontOfSize:13.f];
        _countLabel.textColor = kBlack333;
    }
    return _countLabel;
}

- (UIButton *)reduceButton{
    if (!_reduceButton) {
        _reduceButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_reduceButton setImage:IMG(@"cutback_icon_default") forState:UIControlStateNormal];
        [_reduceButton addTarget:self action:@selector(onClickReduceBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_addButton setImage:IMG(@"buy_icon_home_default") forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(onClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5f)];
        _bottomLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _bottomLine;
}


- (UIImageView *)invalidTitleImgView{
    if (!_invalidTitleImgView) {
        _invalidTitleImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 14.f, 14.f)];
        _invalidTitleImgView.image = IMG(@"cartlist_caveat_icon");
    }
    return _invalidTitleImgView;
}

- (UILabel *)invalidTitleLabel{
    if (!_invalidTitleLabel) {
        _invalidTitleLabel = [[UILabel alloc] init];
        _invalidTitleLabel.backgroundColor = [UIColor clearColor];
        _invalidTitleLabel.font = [UIFont systemFontOfSize:10.f];
        _invalidTitleLabel.textColor = kBlack999;
        _invalidTitleLabel.text = @"因配送范围、库存等原因导致以下商品咱无法购买";
        [_invalidTitleLabel sizeToFit];
    }
    return _invalidTitleLabel;
}


- (UILabel *)invalidReasonLabel{
    if (!_invalidReasonLabel) {
        _invalidReasonLabel = [[UILabel alloc] init];
        _invalidReasonLabel.backgroundColor = [UIColor clearColor];
        _invalidReasonLabel.font = [UIFont systemFontOfSize:12.f];
        _invalidReasonLabel.textColor = kBlack999;
    }
    return _invalidReasonLabel;
}

@end
